package com.example.beerhunterapp;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TableRow;
import android.widget.TextView;

public class SearchWindow extends Activity{
	
	Intent intentMenu;
	Intent intentScan;
	Intent intentBeerLog;
	Intent intentChallenge;
	Intent intentVisualisation;
	MediaPlayer mp1;
	MediaPlayer mp2;
	boolean soundPlay=true;
	Spinner origineList;
	CheckBox parmisBue;
	EditText recherche;
	LayoutInflater factory;
	View duvelView ;
	View heinekenView ;
	View hoegaardenView ;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_activity);
        
        mp1 = MediaPlayer.create(this, R.raw.gunload); 
        mp2 = MediaPlayer.create(this, R.raw.gunshot); 
        intentMenu = new Intent(this, MenuWindow.class);
        intentScan = new Intent(this, ScanWindow.class);
        intentBeerLog = new Intent(this, BeerlogWindow.class);
        intentChallenge = new Intent(this, ChallengeWindow.class);
        intentVisualisation = new Intent(this, VisualisationWindow.class);
        factory = getLayoutInflater();
        duvelView = factory.inflate(R.layout.duvel_textview, null);
        heinekenView = factory.inflate(R.layout.heineken_textview, null);
        hoegaardenView = factory.inflate(R.layout.hoegaarden_textview, null);
        recherche = (EditText) findViewById(R.id.searchBar);        
        
        if(InfoProtoypeSingleton.getInstance().getNumberOfBeer()!=2){
        	TextView t = (TextView)findViewById(R.id.beerLogScoreMenuTextView);
        	t.setText(getResources().getText(R.string.beerlogscorenew));
        }
        
        origineList = (Spinner) findViewById(R.id.origineList);
    	List<String> list = new ArrayList<String>();
    	list.add(getResources().getString(R.string.toutOrigineText));
    	list.add(getResources().getString(R.string.belgiqueText));
    	list.add(getResources().getString(R.string.paysbasText));
    	ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
    		R.layout.spinner_item, list);
    	dataAdapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
    	origineList.setAdapter(dataAdapter);
        origineList.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View arg1,
					int arg2, long arg3) {
				InfoProtoypeSingleton.getInstance().setOrigine(parent.getItemAtPosition(arg2).toString());
				recherche.setText("");
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
        
        parmisBue = (CheckBox) findViewById(R.id.parmiBuCheckBox);
        parmisBue.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(parmisBue.isChecked()){
					InfoProtoypeSingleton.getInstance().setParmisBue(true);
					recherche.setText("");
				}
				else{
					InfoProtoypeSingleton.getInstance().setParmisBue(false);
					recherche.setText("");
				}
			}
		});
        
        findViewById(R.id.duvelRow).setOnTouchListener(new OnTouchListener(){
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == (MotionEvent.ACTION_UP)){
	                //Do whatever you want after press
					((TableRow)v).setBackgroundResource(R.color.white);
					TextView firstTextView = (TextView) ((TableRow)v).getChildAt(0);
					InfoProtoypeSingleton.getInstance().setBeerToSee(firstTextView.getText().toString());
					startActivity(intentVisualisation);
					finish();
	            }
	            else{
	                //Do whatever you want during press  
	            	((TableRow)v).setBackgroundResource(R.color.greenArmy);
	            }
	            return true;
			}
        	
        });
        findViewById(R.id.heinekenRow).setOnTouchListener(new OnTouchListener(){
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == (MotionEvent.ACTION_UP)){
	                //Do whatever you want after press
					((TableRow)v).setBackgroundResource(R.color.white);
					TextView firstTextView = (TextView) ((TableRow)v).getChildAt(0);
					InfoProtoypeSingleton.getInstance().setBeerToSee(firstTextView.getText().toString());
					startActivity(intentVisualisation);
					finish();
	            }
	            else{
	                //Do whatever you want during press  
	            	((TableRow)v).setBackgroundResource(R.color.greenArmy);
	            }
	            return true;
			}
        	
        });
        findViewById(R.id.hoegaardenRow).setOnTouchListener(new OnTouchListener(){
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == (MotionEvent.ACTION_UP)){
	                //Do whatever you want after press
					((TableRow)v).setBackgroundResource(R.color.white);
					TextView firstTextView = (TextView) ((TableRow)v).getChildAt(0);
					InfoProtoypeSingleton.getInstance().setBeerToSee(firstTextView.getText().toString());
					startActivity(intentVisualisation);
					finish();
	            }
	            else{
	                //Do whatever you want during press  
	            	((TableRow)v).setBackgroundResource(R.color.greenArmy);
	            }
	            return true;
			}
        	
        });
        recherche.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				((TableRow)findViewById(R.id.duvelRow)).removeView(findViewById(R.id.duvel_textview));
				((TableRow)findViewById(R.id.heinekenRow)).removeView(findViewById(R.id.heineken_textview));
				((TableRow)findViewById(R.id.hoegaardenRow)).removeView(findViewById(R.id.hoegaarden_textview));
				((TableRow)findViewById(R.id.duvelRow)).addView(duvelView.findViewById(R.id.duvel_textview));
				((TableRow)findViewById(R.id.heinekenRow)).addView(heinekenView.findViewById(R.id.heineken_textview));
				((TableRow)findViewById(R.id.hoegaardenRow)).addView(hoegaardenView.findViewById(R.id.hoegaarden_textview));
				
				String rechercheText = recherche.getText().toString().toLowerCase();
				int textSize = rechercheText.length();
				if(textSize==0){
					((TableRow)findViewById(R.id.duvelRow)).removeView(findViewById(R.id.duvel_textview));
					((TableRow)findViewById(R.id.heinekenRow)).removeView(findViewById(R.id.heineken_textview));
					((TableRow)findViewById(R.id.hoegaardenRow)).removeView(findViewById(R.id.hoegaarden_textview));
				}
				else if(textSize<="duvel".length() && "duvel".substring(0,textSize).equals(rechercheText)){
					((TableRow)findViewById(R.id.heinekenRow)).removeView(findViewById(R.id.heineken_textview));
					((TableRow)findViewById(R.id.hoegaardenRow)).removeView(findViewById(R.id.hoegaarden_textview));
				}
				else if(textSize==1 && rechercheText.equals("h")){
					((TableRow)findViewById(R.id.duvelRow)).removeView(findViewById(R.id.duvel_textview));
				}
				else if(textSize<="heineken".length() && "heineken".substring(0,textSize).equals(rechercheText)){
					((TableRow)findViewById(R.id.duvelRow)).removeView(findViewById(R.id.duvel_textview));
					((TableRow)findViewById(R.id.hoegaardenRow)).removeView(findViewById(R.id.hoegaarden_textview));
				}
				else if(textSize<="hoegaarden".length() && "hoegaarden".substring(0,textSize).equals(rechercheText)){
					((TableRow)findViewById(R.id.duvelRow)).removeView(findViewById(R.id.duvel_textview));
					((TableRow)findViewById(R.id.heinekenRow)).removeView(findViewById(R.id.heineken_textview));
				}
				else{
					((TableRow)findViewById(R.id.duvelRow)).removeView(findViewById(R.id.duvel_textview));
					((TableRow)findViewById(R.id.heinekenRow)).removeView(findViewById(R.id.heineken_textview));
					((TableRow)findViewById(R.id.hoegaardenRow)).removeView(findViewById(R.id.hoegaarden_textview));
				}
				
				if(InfoProtoypeSingleton.getInstance().getNumberOfBeer()==2 && InfoProtoypeSingleton.getInstance().isParmisBue()){
					((TableRow)findViewById(R.id.duvelRow)).removeView(findViewById(R.id.duvel_textview));
				}
				if(InfoProtoypeSingleton.getInstance().getOrigine().equals("Pays-Bas")){
					((TableRow)findViewById(R.id.duvelRow)).removeView(findViewById(R.id.duvel_textview));
					((TableRow)findViewById(R.id.hoegaardenRow)).removeView(findViewById(R.id.hoegaarden_textview));
				}
				if(InfoProtoypeSingleton.getInstance().getOrigine().equals("Belgique")){
					((TableRow)findViewById(R.id.heinekenRow)).removeView(findViewById(R.id.heineken_textview));
				}

			}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}
			
			@Override
			public void afterTextChanged(Editable s) {
			}
		});
        
        ImageView scanButton = (ImageView) findViewById(R.id.scanButton);
        scanButton.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View view, MotionEvent event) {
				if(event.getAction() == (MotionEvent.ACTION_UP)){
	                //Do whatever you want after press
					((ImageView)view).setImageResource(R.drawable.scanboutonshot);
					mp2.start();
					startActivity(intentScan);
					finish();
	            }
	            else{
	                //Do whatever you want during press  
	            	((ImageView)view).setImageResource(R.drawable.scanbouton2);
	            	if(soundPlay){
		            	mp1.start();
		            	soundPlay=false;
	            	}     	        
	            }
	            return true;
			}

		});
        
        Button beerLogButton = (Button) findViewById(R.id.beerLogButton);
        beerLogButton.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				startActivity(intentBeerLog);
				finish();
			}
        });
        
        Button challengeButton = (Button) findViewById(R.id.challengeButton);
        challengeButton.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				startActivity(intentChallenge);
				finish();
			}
        });
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (keyCode == KeyEvent.KEYCODE_BACK ) {
            // do something on back.
			startActivity(intentMenu);
			finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }   

}
